package com.u2b.db.user;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import orm.AddProxy;
import orm.QueryProxy;
import orm.SqlCreate;
import orm.UpdateProxy;

public class UserDaoImpl
{

    public UserDaoImpl()
    {

    }

    public int updateUser(HashMap<String, Object> value)
    {
       
        String sql = SqlCreate.createUpdateSql("user_volatile_db", "uid", value);
        String[] sqlKeys = SqlCreate.createSqlKeys("uid", value);
        System.out.println(sql);
        System.out.println(Arrays.asList(sqlKeys));
        System.out.println("updateUser   uid :" + value.get("user_name"));
        int flag = new UpdateProxy().update(sql, sqlKeys, value);
        return flag;
    }

    public HashMap<String, Object> login(HashMap<String, Object> loginInfo)
    {
    	
        String sql = "select user_static_db.birthday,user_static_db.gender,user_static_db.create_time,user_volatile_db.* from user_static_db,user_volatile_db where  user_static_db.uid=user_volatile_db.uid and user_static_db.uid in  (select  uid  from  user_static_db  where email=? and password=?)";
        String[] sqlKeys = new String[]{ "email", "password" };
        HashMap<String, Object> userHashMap = new QueryProxy().querySingle(sql, sqlKeys, loginInfo);
        return userHashMap;
    }

    public HashMap<String, Object> getUserByUid(int uid)
    {

        String sql = "select user_static_db.birthday,user_static_db.gender,user_static_db.create_time,user_volatile_db.* from user_static_db,user_volatile_db where  user_static_db.uid=user_volatile_db.uid and user_volatile_db.uid=?";

        HashMap<String, Object> user = new HashMap<String, Object>();
        user = new QueryProxy().querySingle(sql, uid);
        return user;

    }

    public int RegisterUser(HashMap<String, Object> value)
    {
    	//这里应该用事物操作,但是add方法中不支持,按理来说应该用Spring来做
        String sql_1 = "insert into user_static_db(birthday,email,create_time,password,gender)values(?,?,?,?,?)";
        String[] sqlKeys = new String[]
        { "birthday", "email", "create_time", "password", "gender" };
        int primaryKey = new AddProxy().add(sql_1, sqlKeys, value);
        if (primaryKey > 0)
        {
            String sql_2 = "insert into user_volatile_db(uid) values(?)";
            String[] keys = new String[]
            { "uid" };
            HashMap<String, Object> values = new HashMap<String, Object>();
            values.put("uid", primaryKey);
            new AddProxy().add(sql_2, keys, values);
        }
    	
    	
        return primaryKey;

    }

    public int setVideo(HashMap<String, Object> value)
    {
        String sql = "update user_volatile_db set video_URL=? where uid =?";
        String[] sqlKeys = new String[]
        { "video_URL", "uid" };
        int flag = new UpdateProxy().update(sql, sqlKeys, value);
        return flag;

    }

    public int setEmail(HashMap<String, Object> value)
    {
        String sql = "update user set email=? where uid=?";
        String[] sqlKeys = new String[]
        { "email", "uid" };
        int flag = new UpdateProxy().update(sql, sqlKeys, value);
        return flag;

    }

    public int setPassword(HashMap<String, Object> value)
    {
        String sql = "update user set password=? where uid=?";
        String[] SQLkeys = new String[]
        { "password", "uid" };
        int flag = new UpdateProxy().update(sql, SQLkeys, value);
        return flag;

    }

    // 本来就是分开的,并不需要合起来查询,new一个也是错误的
    public List<HashMap<String, Object>> getMyZones(int uid)
    {
        String sql_1 = "select * from  status_db where owner=?";
        String sql_2 = "select * from  activity_db where owner=?";
        List<HashMap<String, Object>> list_status = new QueryProxy().query(sql_1, uid);
        List<HashMap<String, Object>> list_activity = new QueryProxy().query(sql_2, uid);
        list_status.addAll(list_activity);
        return list_status;

    }
}